<?php

require("twitteroauth/twitteroauth.php");
require_once 'userManager.php';
session_start();

if ($_GET['submit'] == "1")
        {


            $encrypted = crypt(md5($_POST['password2']), md5($_POST['username2']));
            $newUser = new User();
            $newUser->setFName($_POST[firstName]);
            $newUser->setLName($_POST[lastName]);
            $newUser->setPassword($encrypted);
            $newUser->setEmail($_POST[email]);
            $newUser->setPhone($_POST[phone]);
            $newUser->setAddress($address);
            $newUser->setBirthday($birthdate);
            $newUser->setOath_id($_POST[newOath_id]);
            $newUser->setOath_provider($_POST[newOath_provider]);
            $newUser->setOath_token($_POST[newOath_token]);
            $newUser->setOath_secret($_POST[newOath_secret]);
            $regedUser = userManager::registerTwitter($newUser);

        }

if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) {
    // We've got everything we need
} else {
    // Something's missing, go back to square 1
    header('Location: twitter_login.php');
}

// TwitterOAuth instance, with two new parameters we got in twitter_login.php
$twitteroauth = new TwitterOAuth('6w9YqMhlUxS6tL1WubERGQ', 'BHlIB2UXBZNE12l41j2uJ0ggy0W0IQ90wjSplnLNE', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Let's request the access token
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
// Save it in a session var
$_SESSION['access_token'] = $access_token;
// Let's get the user's info
$user_info = $twitteroauth->get('account/verify_credentials');
// Print user's info
print_r($user_info);

mysql_connect('pluto.cse.msstate.edu', 'dcspg8', 'ab1234');
mysql_select_db('dcspg8');

if (isset($user_info->error)) {
    // Something's wrong, go back to square 1
    header('Location: twitter_login.php');
} else {

//    foreach ($user_info as $key => $data) {
//        echo $key . " = " . $data . "<br>";
//    }
    // Let's find the user by its ID
    $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'twitter' AND oauth_uid = " . $user_info->id);
//    $result = mysql_fetch_array($query);

    // If not, let's add it to the database
    if (empty($result)) {

          $oauth_token = $_GET['oauth_token'];
        $oauth_secret = $_GET['id_str'];
        $oauth_verifier = $_GET['oauth_verifier'];
        $oauth_id = $user_info->id;


        $registerForm = "<form method=\"POST\" action=\"twitter_oath.php?submit=1\">
    <table>

        <input type=\"hidden\" name=\"oauth_token\" value=\"$oauth_token\">
        <input type=\"hidden\" name=\"oauth_secret\" value=\"$oauth_secret\">
        <input type=\"hidden\" name=\"oauth_provider\" value=\"$oauth_provider\">
        <input type=\"hidden\" name=\"oauth_id\" value=\"$oauth_id\">

    <tr><td>*First Name:</td><td><input type=\"text\" name=\"firstName\"></td></tr>
    <tr><td>Last Name:</td><td><input type=\"text\" name=\"lastName\"></td></tr>
    <tr><td>*Password:</td><td><input type=\"password\" name=\"password2\" /></td></tr>
    <tr><td>*Re-enter Password:</td><td><input type=\"password\" name=\"password3\" /></td></tr>
    <tr><td>*Email:</td><td><input type=\"text\" name=\"email\"></td></tr>
    <tr><td>*Birth Date (MM/DD/YYYY):</td><td>

<select name=\"birthMonth\">
                        <option value=\"01\">January</option>
                        <option value=\"02\">February</option>
                        <option value=\"03\">March</option>
                        <option value=\"04\">April</option>
                        <option value=\"05\">May</option>
                        <option value=\"06\">June</option>
                        <option value=\"07\">July</option>
                        <option value=\"08\">August</option>
                        <option value=\"09\">September</option>
                        <option value=\"10\">October</option>
                        <option value=\"11\">November</option>
                        <option value=\"12\">December</option>
                    </select>
                    <select name=\"birthDay\">
                        <option value=\"01\">01</option>
                        <option value=\"02\">02</option>
                        <option value=\"03\">03</option>
                        <option value=\"04\">04</option>
                        <option value=\"05\">05</option>
                        <option value=\"06\">06</option>
                        <option value=\"07\">07</option>
                        <option value=\"08\">08</option>
                        <option value=\"09\">09</option>
                        <option value=\"10\">10</option>
                        <option value=\"11\">11</option>
                        <option value=\"12\">12</option>
                        <option value=\"13\">13</option>
                        <option value=\"14\">14</option>
                        <option value=\"15\">15</option>
                        <option value=\"16\">16</option>
                        <option value=\"17\">17</option>
                        <option value=\"18\">18</option>
                        <option value=\"19\">19</option>
                        <option value=\"20\">20</option>
                        <option value=\"21\">21</option>
                        <option value=\"22\">22</option>
                        <option value=\"23\">23</option>
                        <option value=\"24\">24</option>
                        <option value=\"25\">25</option>
                        <option value=\"26\">26</option>
                        <option value=\"27\">27</option>
                        <option value=\"28\">28</option>
                        <option value=\"29\">29</option>
                        <option value=\"30\">30</option>
                        <option value=\"31\">31</option>
                    </select>

<input type=\"text\" name=\"birthYear\" size=4/>
</td></tr>
    <tr><td>Sex:</td><td>Gal <input type=\"radio\" name=\"sex\" value=\"F\"  /> Guy <input type=\"radio\" name=\"sex\" value=\"M\" /></td></tr>
    <tr><td>Phone Number:</td><td><input type=\"text\" name=\"phone\"/></td></tr>
    <tr><td>Street:</td><td><input type=\"text\" name=\"street\"/></td></tr>
    <tr><td>Zip Code:</td><td><input type=\"text\" name=\"zip\"></td></tr>
    <tr><td>State:</td><td><input type=\"text\" name=\"state\"></td></tr>
    <tr><td>School:</td><td><input type=\"text\" name=\"school\"></td></tr>
    <tr><td><center><input type=\"reset\" value=\"Reset Values\" /></center></td><td><center><input name=\"register\" type=\"submit\" value=\"Register\"></center></td></tr>
    </table></form>
    ";
        

    } else {
        // Update the tokens
        $query = mysql_query("UPDATE users SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_uid = {$user_info->id}");
    }

//    $_SESSION['id'] = $result['id'];
////    $_SESSIOM['firstName'] = $result['name'];
//    $_SESSION['username'] = $result['username'];
////    $_SESSION['oauth_uid'] = $result['oauth_uid'];
//    $_SESSION['oauth_provider'] = $result['oauth_provider'];
//    $_SESSION['oauth_token'] = $result['oauth_token'];
//    $_SESSION['oauth_secret'] = $result['oauth_secret'];


//    foreach ($result as $key => $data) {
//        echo $key . "=" . $data . "<br>";
//    }

    if (!empty($_SESSION['username'])) {
        // User is logged in, redirect
//    header('Location: index.php');
    }
}
?>
